package com.lianxian.xindong.home.mvp.ui.holder;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.YLCircleImageView;
import com.hxb.library.base.BaseHolder;
import com.hxb.library.base.DefaultAdapter;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.http.imageloader.ImageLoader;
import com.hxb.library.utils.AppManagerUtil;
import com.hxb.library.utils.HxbUtils;
import com.hxb.library.utils.TimeUtils;
import com.lianxian.xindong.commonres.picture.lookImgs.LookImgsUtils;
import com.lianxian.xindong.commonsdk.BuildConfig;
import com.lianxian.xindong.commonsdk.imgaEngine.config.CommonImageConfigImpl;
import com.lianxian.xindong.home.R;
import com.lianxian.xindong.home.R2;
import com.lianxian.xindong.home.mvp.model.entity.CircleBean;
import com.lianxian.xindong.home.mvp.ui.adapter.CircleImgAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author: HeXingBo
 * @date: 2022/11/15
 * @description：
 */
public class CircleItemHolder extends BaseHolder<CircleBean> {

    @BindView(R2.id.iv_head)
    YLCircleImageView ivHead;
    @BindView(R2.id.tv_nice)
    TextView tvNice;
    @BindView(R2.id.tv_sex_and_age)
    TextView tvSexAndAge;
    @BindView(R2.id.tv_address_and_job)
    TextView tvAddressAndJob;
    @BindView(R2.id.tv_hobby)
    TextView tvHobby;
    @BindView(R2.id.tv_des)
    TextView tvDes;
    @BindView(R2.id.rcy_img)
    RecyclerView rcyImg;
    @BindView(R2.id.tv_time)
    TextView tvTime;
    @BindView(R2.id.tv_zan)
    TextView tvZan;
    @BindView(R2.id.tv_msg_drop)
    TextView tvMsgDrop;
    @BindView(R2.id.tv_msg_hi)
    TextView tvMsgHi;

    private AppComponent mAppComponent;
    private ImageLoader mImageLoader;//用于加载图片的管理类,默认使用 Glide,使用策略模式,可替换框架

    public CircleItemHolder(View itemView) {
        super(itemView);
        //可以在任何可以拿到 Context 的地方,拿到 AppComponent,从而得到用 Dagger 管理的单例对象
        mAppComponent = HxbUtils.obtainAppComponentFromContext(itemView.getContext());
        mImageLoader = mAppComponent.imageLoader();
    }

    @Override
    public void setData(@NonNull CircleBean data, int position) {

        tvNice.setText("昵称" + (position + 1));
        tvSexAndAge.setText("年龄" + (position + 1));
        tvAddressAndJob.setText("地区" + (position + 1) + "·" + "职业" + (position + 1));
        tvHobby.setText("兴趣爱好 | " + (position + 1));
        tvDes.setText("动态内容描述" + (position + 1));
        tvTime.setText(TimeUtils.getNowTimeString());
        if (position % 2 == 1) {
            rcyImg.setVisibility(View.GONE);
        } else {
            rcyImg.setVisibility(View.VISIBLE);
            List<String> circleImgBeans = new ArrayList<>();
            if (position + 1 > 9) {
                position = 0;
            }
            for (int i = 0; i < position + 1; i++) {
                if (i > 9) {
                    break;
                }
                circleImgBeans.add(BuildConfig.DEFAULT_IMG_URL);
            }
            CircleImgAdapter circleImgAdapter = new CircleImgAdapter(circleImgBeans);
            rcyImg.setLayoutManager(new GridLayoutManager(rcyImg.getContext(), 3));
            rcyImg.setAdapter(circleImgAdapter);
            circleImgAdapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<String>() {
                @Override
                public void onItemClick(@NonNull View view, int viewType, @NonNull String data, int position) {
                    //图片预览
                    LookImgsUtils.init().lookImgs(AppManagerUtil.getCurrentActivity(), circleImgBeans, position);
                }
            });
        }
        //itemView 的 Context 就是 Activity, Glide 会自动处理并和该 Activity 的生命周期绑定
        mImageLoader.loadImage(itemView.getContext(),
                CommonImageConfigImpl.builder()
                        .url(TextUtils.isEmpty("url") ? BuildConfig.DEFAULT_HEAD_URL : "url")
                        .errorPic(R.drawable.ic_defunct_head)
                        .placeholder(R.drawable.ic_defunct_head)
                        .imageRadius(10)
                        .imageView(ivHead)
                        .build());
    }

    @Override
    protected void onRelease() {
        mImageLoader.clear(mAppComponent.application(), CommonImageConfigImpl.builder()
                .imageViews(ivHead)
                .build());
    }
}